package com.csis.system.common.convert;

import com.csis.framework.domain.FormBean;
import com.csis.framework.util.jqgrid.JqGridModel;
import com.csis.system.common.WebUtil;
import com.csis.system.common.convert.Converter.ConvertHandler;

public class FormBeanConvertHandler implements ConvertHandler {
	private JqGridModel model;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public <T> T handTarget(Class<T> clazz) {
		if (clazz == FormBean.class || clazz.getSuperclass() == FormBean.class) {
			try {
//				System.out
//						.println(model.getSearchField() + " " + model.getSearchOper() + " " + model.getSearchString());
				FormBean formBean = (FormBean) clazz.newInstance();
				formBean.getPage().setOrderBy(model.getSidx());
				formBean.getPage().setSortBy(model.getSord());
				formBean.getPage().setCurPage(WebUtil.buildConvert(model.getPage()).convertToSingle(int.class, 0));
				formBean.getPage().setCount(WebUtil.buildConvert(model.getRows()).convertToSingle(int.class, 10));
				if (model.isSearch())
					formBean.setCondition(model.getSearchCondition());
				formBean.setIsAll(model.getIsAll());
				return (T) formBean;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void handSource(Object obj) {
		model = (JqGridModel) obj;
	}

}
